import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios";
//挂载Vuex
Vue.use(Vuex)
import qs from 'qs'

//创建VueX对象
const store  = new Vuex.Store({
 state:{
    //存放的键值对就是所要管理的状态
    userlist:[],
    assetsList:[]
    },
  mutations:{
   changePagesize(state,pagesize){
        state.pagesize=pagesize
      },
    changePageno(state,pageno){
       state.pageno=pageno
    },
    setUserList(state,userList){
     state.userlist = userList
    },
    setAssetList(state,assetsList){
      state.assetsList = assetsList
    }

  },
  actions:{
   getUserList(context){
     axios.get('/api/user/').then(
       res=>{
         context.commit('setUserList',res.data)
       }
     )
   },
    getAssetsList(context){
     axios.get('/api/assets/').then(
       res=>{
        context.commit('setAssetList',res.data)
       }
     )
    },
      //获取总条数
    gettotal(context){
      axios.post('/api/search/',qs.stringify({
        type:context.state.searchType,
        keyword:context.state.keyword,
      }),{
        headers:{
          'content-type': 'application/x-www-form-urlencoded',
        },
        }
      ).then(res=>{
        console.log(res.data)
          context.state.tableData[context.state.searchType]['datalist']=res.data.data
          context.state.total=res.data.count
      })
    },
    getdata(context){
      //开发环境
      // const searchURI = '/api/search/';
      //生产环境
      const searchURI = '/search/';
      axios.post(searchURI,qs.stringify({
        type:context.state.searchType,
        keyword:context.state.keyword,
        pageno:context.state.pageno,
        pagesize:context.state.pagesize,
      }),{
        headers:{
          'content-type': 'application/x-www-form-urlencoded',
          "token":'14a1347f412b39f'
        },
        }
      ).then(res=>{
          console.log(res.data)
          context.state.tableData[context.state.searchType]['datalist']=res.data.data
          context.state.total=res.data.total
          console.log("获取到条数：",context.state.total)
      })
    }
  }
})

export default store
